package com.polito.mad.bikeponyapp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Application;
import android.content.res.AssetManager;
import android.graphics.Typeface;

import com.polito.mad.bikeponyapp.persistence.DeliveriesRepository;
import com.polito.mad.bikeponyapp.persistence.QuestionsRepository;
import com.polito.mad.bikeponyapp.persistence.RepositoryHolder;

public class BikePonyApplication extends Application implements RepositoryHolder {
	
	private DeliveriesRepository deliveriesRepository;
	private QuestionsRepository questionsRepository;
	private Map<Long,List<String>>mapEncodedPaths;
	@Override
	public void onCreate() {
		super.onCreate();
		try {
			this.deliveriesRepository =new DeliveriesRepository(this);
			mapEncodedPaths = new HashMap<Long, List<String>>();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void onTerminate() {
		super.onTerminate();
		
		TypeFaceLoader.getInstance(getAssets());
	}
	
	@Override
	public DeliveriesRepository getDeliveriesRepository() {
		return deliveriesRepository;
	}
	
	
	/**
	 * util class to load the Roboto fonts from assets
	 * 
	 * @author nicola
	 *
	 */
	public static class TypeFaceLoader {

		private Typeface robotoLightTypeFace;
		private Typeface robotoMediumTypeFace;
		private Typeface robotoRegularTypeFace;
		private Typeface robotoThinTypeFace;
		
		public TypeFaceLoader(AssetManager assetManager) {
			robotoLightTypeFace = Typeface.createFromAsset(assetManager, "fonts/Roboto-Light.ttf");
			robotoMediumTypeFace = Typeface.createFromAsset(assetManager, "fonts/Roboto-Medium.ttf");
			robotoRegularTypeFace = Typeface.createFromAsset(assetManager, "fonts/Roboto-Regular.ttf");
			robotoThinTypeFace = Typeface.createFromAsset(assetManager, "fonts/Roboto-Thin.ttf");
		}

		
		private static TypeFaceLoader typeFaceLoader;
		
		public static TypeFaceLoader getInstance(AssetManager assetManager)
		{
			if(typeFaceLoader==null)
				typeFaceLoader = new TypeFaceLoader(assetManager);
			return typeFaceLoader;
		}
		
		
		
		public Typeface getRobotoLightTypeFace() {
			return robotoLightTypeFace;
		}

		public Typeface getRobotoMediumTypeFace() {
			return robotoMediumTypeFace;
		}

		public Typeface getRobotoRegularTypeFace() {
			return robotoRegularTypeFace;
		}

		public Typeface getRobotoThinTypeFace() {
			return robotoThinTypeFace;
		}
	}


	@Override
	public synchronized QuestionsRepository getQuestionsRepository() {
		if(questionsRepository==null)
		{
			try {
				questionsRepository= new QuestionsRepository(this);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return questionsRepository;
	}

	public List<String> getMapEncodedPath(Long id) {
		return mapEncodedPaths.get(id);
	}
	
	public void addMacEncodedPath(Long id,List<String> path)
	{
		mapEncodedPaths.put(id, path);
	}

	public void resetMapEncodedPath() {
		mapEncodedPaths.clear();
	}
	

}
